# 连接数据库
import pymysql, json

conn = pymysql.Connect(
    host='47.94.130.233',
    port=3306,
    user='root',
    passwd='123456',
    db='aaa',
    charset='utf8',
)
# print(conn)
# 获取游标
my_cursor = conn.cursor()
# 准备SQL
sql = 'SELECT * FROM area LIMIT 4000'
# 准备插入的数据
# 执行SQL
my_cursor.execute(sql)
# 获取所有记录
rs = my_cursor.fetchall()
# 关闭连接
conn.close()
addresses = []
for address in rs:
    addresses.append({
        'region_code': address[0],
        'region_name': address[1],
        'parent_code': address[2],
    })
with open(file='地址.json', mode='w', encoding='utf-8') as f:
    json.dump(addresses, f, ensure_ascii=False)
